package com.zxq.users.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zxq.common.pojo.ApiResponse;
import com.zxq.common.service.BaseServiceImpl;
import com.zxq.common.util.ConstatFinalUtil;
import com.zxq.users.pojo.APosition;
import com.zxq.users.pojo.AUsers;
import com.zxq.users.pojo.AUsersEnum;
import com.zxq.users.service.IUsersDbService;
import com.zxq.users.service.IUsersOperService;

/**
 * 用户的业务相关的service
 *
 * @author 章晓琦
 */
@Service("usersOperService")
public class UsersOperServiceImpl extends BaseServiceImpl implements IUsersOperService
{
	@Resource
	private IUsersDbService usersDbService;

	/**
	 * 当前用户的类型:
	 * 		一级执法人员:上级是部门领导;无下级
	 * 		部门领导:上级局领导,下级:一级执行人员
	 * 		局领导:上级:无,下级:部门领导;
	 * 职位表里面的code码不能变
	 */
	@Override
	public List<AUsers> findCondListTreeUsersService(Map<String, Object> condMap)
	{
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		/* 准备一个集合 */
		List<AUsers> usersList = new ArrayList<AUsers>();
		if(condMap.get("operType") != null && !"".equalsIgnoreCase(condMap.get("operType") + ""))
		{
			String operType = condMap.get("operType") + "";
			String usersId = condMap.get("usersId") + "";
			/* 查询当前用户的职位 */
			paramsMap.clear();
			paramsMap.put("id", usersId);
			ApiResponse<AUsers> usersApiResponse = this.usersDbService.findOneUsersService(paramsMap);
			/* 判断用户的类型 */
			AUsers users = usersApiResponse.getDataOneJava();
			/* 当前用户所在的部门 */
			int departId = users.getPosition().getDepartId();
			
			/* 上级码 */
			String upCode = "" ;
			/* 下级码 */
			String downCode = "";
			
			/* 用户的职位 */
			APosition position = users.getPosition() ; 
			if(ConstatFinalUtil.POSITION_ENF_PER.equalsIgnoreCase(position.getCode()))
			{
				/* 当前用户为一线执法人员 */
				upCode = ConstatFinalUtil.POSITION_DPART_LEADER;
			}else if(ConstatFinalUtil.POSITION_DPART_LEADER.equalsIgnoreCase(position.getCode()))
			{
				/* 当前用户为部门领导 */
				upCode = ConstatFinalUtil.POSITION_BUREAU_LEADER;
				downCode = ConstatFinalUtil.POSITION_ENF_PER ; 
			}else if(ConstatFinalUtil.POSITION_BUREAU_LEADER.equalsIgnoreCase(position.getCode()))
			{
				/* 当前用户为局领导 */
				downCode = ConstatFinalUtil.POSITION_DPART_LEADER ; 
			}
			
			/* 根据码查询职位 */
			paramsMap.clear();
			if(operType.equalsIgnoreCase(AUsersEnum.OPERTYPE_TREE_DOWN.getStatus() + "") && !"".equalsIgnoreCase(downCode))
			{
				/* 查询下级 */
				paramsMap.put("code", downCode);
			}else if(operType.equalsIgnoreCase(AUsersEnum.OPERTYPE_TREE_UP.getStatus() + "") && !"".equalsIgnoreCase(upCode))
			{
				/* 查询上级 */
				paramsMap.put("code", upCode);
			}
			
			/* 必须限定在同一个部门 */
			paramsMap.put("departId", departId);
			
			if(paramsMap.size() > 0 )
			{
				/* 查询部门有多个 */
				ApiResponse<APosition> positionListApiResponse = this.usersDbService.findCondListPositionService(null, paramsMap);
				List<APosition> positionList = positionListApiResponse.getDataListJava() ; 
				
				if(positionList.size() > 0)
				{
					List<String> positionIdList = new ArrayList<String>();
					for (Iterator iterator = positionList.iterator(); iterator.hasNext();)
					{
						APosition positionTemp = (APosition) iterator.next();
						positionIdList.add(positionTemp.getId() + "");
					}
					/* 只需要查询到这个部门下面的用户即可 */
					paramsMap.clear();
					paramsMap.put("positionIdList", positionIdList);
					ApiResponse<AUsers> usersTarApiResponse = this.usersDbService.findCondListUsersService(null, paramsMap);
					usersList = usersTarApiResponse.getDataListJava() ; 
				}
			}
		}
		return usersList;
	}

	@Override
	public List<AUsers> findCondListDepartUsersService(Map<String, Object> condMap)
	{
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		/* 准备一个集合 */
		List<AUsers> usersList = new ArrayList<AUsers>();
		String usersId = condMap.get("usersId") + "";
		/* 查询当前用户的职位 */
		paramsMap.clear();
		paramsMap.put("id", usersId);
		ApiResponse<AUsers> usersApiResponse = this.usersDbService.findOneUsersService(paramsMap);
		/* 判断用户的类型 */
		AUsers users = usersApiResponse.getDataOneJava();
		/* 当前用户所在的部门 */
		int departId = users.getPosition().getDepartId();
		
		/* 必须限定在同一个部门 */
		paramsMap.put("departId", departId);
		
		if(paramsMap.size() > 0 )
		{
			/* 查询部门有多个 */
			ApiResponse<APosition> positionListApiResponse = this.usersDbService.findCondListPositionService(null, paramsMap);
			List<APosition> positionList = positionListApiResponse.getDataListJava() ; 
			
			if(positionList.size() > 0)
			{
				List<String> positionIdList = new ArrayList<String>();
				for (Iterator iterator = positionList.iterator(); iterator.hasNext();)
				{
					APosition positionTemp = (APosition) iterator.next();
					positionIdList.add(positionTemp.getId() + "");
				}
				/* 只需要查询到这个部门下面的用户即可 */
				paramsMap.clear();
				paramsMap.put("positionIdList", positionIdList);
				ApiResponse<AUsers> usersTarApiResponse = this.usersDbService.findCondListUsersService(null, paramsMap);
				usersList = usersTarApiResponse.getDataListJava() ; 
			}
		}
		return usersList;
	}

}
